import {
  Patch,
  Delete,
  Controller,
  Get,
  Post,
  Body,
  Param,
  Request,
  Query,
  Headers,
  HttpCode,
  Res,
  Req,
  Version,
  Inject,
  ParseIntPipe,
  ParseUUIDPipe,
} from '@nestjs/common';
import { DemoService } from './demo.service';
import { Demo2Service } from './demo2.service';
import { CreateDemoDto } from './dto/create-demo.dto';
import { UpdateDemoDto } from './dto/update-demo.dto';
import { ListService } from '../list/list.service'; //1 引入

import * as svgCaptcha from 'svg-captcha';
import * as uuid from 'uuid';

import { ApiOperation, ApiTags } from '@nestjs/swagger';
// @Controller('demo')
@Controller({
  //版本控制
  path: 'demo',
  version: '1',
})
// http://localhost:3000/demo
// http://localhost:3000/v1/demo
@ApiTags('demo模块 v1/demo')
export class DemoController {
  constructor(
    @Inject('DemoSyntacticSugar') private readonly demoService: DemoService,
    @Inject('Constant') private shopList: string[], //注入常量
    @Inject('Test') private readonly test: any, //工厂模式
    private readonly demo2Service: Demo2Service,
    @Inject('Sync') private syncFun: any, //工厂模式
    private readonly listService: ListService, //2 全局直接注入
  ) {}

  @Get('userList') //@Global 全局共享的list,不会调用list的controllers
  @ApiOperation({
    summary: 'demo模块，全局共享的list',
  })
  getHelloList(): string {
    return this.listService.findAll(); //3 使用
  }
  @Post()
  create(@Body() createDemoDto: CreateDemoDto) {
    return this.demoService.create(createDemoDto);
  }
  @Get('code')
  @ApiOperation({
    summary: '生成二维码',
  })
  createCaptcha(@Req() req, @Res() res) {
    const captcha = svgCaptcha.create({
      size: 4, //生成几个验证码
      fontSize: 50, //文字大小
      width: 100, //宽度
      height: 34, //高度
      background: '#cc9966', //背景颜色
    });
    req.session.code = captcha.text; //存储验证码记录到session
    res.type('image/svg+xml');
    res.send(captcha.data);
  }

  @Post('create')
  @ApiOperation({
    summary: '校验验证码',
  })
  createUser(@Req() req, @Body() body) {
    console.log(req.session.code, body);
    if (
      req.session.code.toLocaleLowerCase() === body?.code?.toLocaleLowerCase()
    ) {
      return {
        message: '验证码正确',
      };
    } else {
      return {
        message: '验证码错误',
      };
    }
  }

  @Get()
  @Version('2') //版本控制-局部
  @ApiOperation({
    summary: '版本控制-局部 v2',
  })
  findAll(@Query() query) {
    console.log('query', query);
    return this.demoService.findAll() + this.demo2Service.doAnotherThing();
  }

  @Get('uid/:id')
  // findOne(@Param('id') id: string) {
  findOneString(@Param('id', ParseUUIDPipe) id: string) {
    console.log('ParseUUIDPipe', typeof id, uuid.v4());
    return uuid.v4();
  }

  @Get(':id')
  @ApiOperation({
    summary: '管道转换',
  })
  // findOne(@Param('id') id: string) {
  findOne(@Param('id', ParseIntPipe) id: number) {
    console.log('ParseIntPipe', typeof id, uuid.v4());
    this.test.doSomething();
    console.log('r', this.syncFun, typeof this.syncFun);
    return this.demoService.findOne(+id) + this.shopList;
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateDemoDto: UpdateDemoDto) {
    return this.demoService.update(+id, updateDemoDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.demoService.remove(+id);
  }
}
